package user

import (
	"chaowan/data_source"
	"chaowan/models"
	"github.com/gin-gonic/gin"
	"net/http"
)

type UserAdderssBody struct {
	ID        int64  `json:"id" gorm:"id"`
	Name      string `json:"name" gorm:"name"`
	Phone     string `json:"phone" gorm:"phone"`
	Province  string `json:"province" gorm:"province"`
	City      string `json:"city" gorm:"city"`
	County    string `json:"county" gorm:"county"` // 区县
	Area      string `json:"area" gorm:"area"`
	Uid       int64  `json:"uid" gorm:"uid"`
	Ifdefault int64  `json:"ifdefault" gorm:"ifdefault"` //1不默认 2默认
}

func UserAdderssadd(ctx *gin.Context) {
	request := UserAdderssBody{}
	ctx.ShouldBind(&request)
	uid := request.Uid
	name := request.Name
	phone := request.Phone
	province := request.Province
	city := request.City
	county := request.County
	area := request.Area
	ifdefault := request.Ifdefault
	if uid == 0 {
		ctx.JSON(http.StatusOK, gin.H{
			"code":    101,
			"error":   1,
			"success": 400,
			"data":    "on",
			"msg":     "请登录",
		})
		return
	}
	if ifdefault == 2 {
		data_source.Db.Model(&models.Address{}).Where("uid = ?", uid).Update("ifdefault", 1)
	}
	addr := models.Address{
		Uid:       uid,
		Name:      name,
		Phone:     phone,
		Province:  province,
		City:      city,
		County:    county,
		Area:      area,
		Ifdefault: ifdefault,
	}
	data_source.Db.Create(&addr)
	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    "ok",
		"msg":     "ok",
	})

}

func UserAdderssupdate(ctx *gin.Context) {
	request := UserAdderssBody{}
	ctx.ShouldBind(&request)
	id := request.ID
	uid := request.Uid
	name := request.Name
	phone := request.Phone
	province := request.Province
	city := request.City
	county := request.County
	area := request.Area
	ifdefault := request.Ifdefault
	if uid == 0 {
		ctx.JSON(http.StatusOK, gin.H{
			"code":    101,
			"error":   1,
			"success": 400,
			"data":    "on",
			"msg":     "请登录",
		})
		return
	}
	if ifdefault == 2 {
		data_source.Db.Model(&models.Address{}).Where("uid = ?", uid).Update("ifdefault", 1)
	}
	addr := models.Address{
		Uid:       uid,
		Name:      name,
		Phone:     phone,
		Province:  province,
		City:      city,
		County:    county,
		Area:      area,
		Ifdefault: ifdefault,
	}
	data_source.Db.Where("id = ?", id).Updates(&addr)
	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    "ok",
		"msg":     "ok",
	})
	return

}

func UserareaInfo(ctx *gin.Context) {
	request := UserAdderssBody{}
	ctx.ShouldBind(&request)
	id := request.ID
	uid := request.Uid
	if uid == 0 {
		ctx.JSON(http.StatusOK, gin.H{
			"code":    101,
			"error":   1,
			"success": 400,
			"data":    "on",
			"msg":     "请登录",
		})
		return
	}
	var address models.Address
	data_source.Db.Where("id = ?", id).First(&address)
	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    address,
		"msg":     "ok",
	})
	return
}

func Areadefault(ctx *gin.Context) {
	request := UserAdderssBody{}
	ctx.ShouldBind(&request)
	uid := request.Uid
	if uid == 0 {
		ctx.JSON(http.StatusOK, gin.H{
			"code":    101,
			"error":   1,
			"success": 400,
			"data":    "on",
			"msg":     "请登录",
		})
		return
	}

	var area models.Address
	data_source.Db.Where("uid = ?", uid).Where("ifdefault = 2").First(&area)
	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    area,
		"msg":     "ok",
	})
	return
}
func Areadefaultswitch(ctx *gin.Context) {
	request := UserAdderssBody{}
	ctx.ShouldBind(&request)
	uid := request.Uid
	id := request.ID
	data_source.Db.Model(&models.Address{}).Where("uid = ?", uid).Update("ifdefault", 1)
	data_source.Db.Model(&models.Address{}).Where("id = ?", id).Update("ifdefault", 2)
	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    "ok",
		"msg":     "ok",
	})
	return
}
func Addrlist(ctx *gin.Context) {
	request := UserAdderssBody{}
	ctx.ShouldBind(&request)
	uid := request.Uid
	if uid == 0 {
		ctx.JSON(http.StatusOK, gin.H{
			"code":    101,
			"error":   1,
			"success": 400,
			"data":    "on",
			"msg":     "请登录",
		})
		return
	}
	var addr []models.Address
	data_source.Db.Where("uid = ?", uid).Find(&addr)
	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    addr,
		"msg":     "ok",
	})
	return
}

func Addrdel(ctx *gin.Context) {
	request := UserAdderssBody{}
	ctx.ShouldBind(&request)
	id := request.ID
	uid := request.Uid
	if uid == 0 {
		ctx.JSON(http.StatusOK, gin.H{
			"code":    101,
			"error":   1,
			"success": 400,
			"data":    "on",
			"msg":     "请登录",
		})
		return
	}
	data_source.Db.Where("id = ?", id).Delete(&models.Address{})

	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    "ok",
		"msg":     "ok",
	})
	return
}
